﻿
lantu.Toolbar = Class.extend({
	
	init:function(elementId, _canvas){
		this.html = $("#"+elementId);
		this.canvas = _canvas;
		// register this class as event listener for the canvas
		// CommandStack. This is required to update the state of 
		// the Undo/Redo Buttons.
		//
		this.canvas.getCommandStack().addEventListener(this);

		// Register a Selection listener for the state hnadling
		// of the Delete Button
		//
        this.canvas.on("select", $.proxy(this.onSelectionChanged,this));
		$("#delBbn").click($.proxy(function(){
						var node = this.canvas.getPrimarySelection();
						var command= new draw2d.command.CommandDelete(node);
						this.canvas.getCommandStack().execute(command);
					},this));
		$("#backBbn").click($.proxy(function(){
						 this.canvas.getCommandStack().undo();
					},this));
		$("#frontBbn").click($.proxy(function(){
				
						
						 this.canvas.getCommandStack().redo();
					},this));
		
			
			
		
		$("#saveBtn").click(function(){
			 var writer = new draw2d.io.json.Writer();
			writer.marshal(_canvas, function(json){
			// convert the json object into string representation
			var jsonTxt = JSON.stringify(json,null,2);
			// insert the json string into a DIV for preview or post
			// it via ajax to the server....
			//离线存储
			
			//同步到远程服务器
			 $.ajax({
             type: "GET",
             url: "syn.json",
             data: {username:$("#username").val(), content:$("#content").val()},
             dataType: "json",
             success: function(data){
                         //弹出成功对话框
						 $('#synHintModal').modal()
                }
			,
			 error:function(){
				  $('#synHintModal').modal('show')
			 }
			});
		 });
		});
	}
	,
	/**
	 * @method
	 * Called if the selection in the cnavas has been changed. You must register this
	 * class on the canvas to receive this event.
	 *
     * @param {draw2d.Canvas} emitter
     * @param {Object} event
     * @param {draw2d.Figure} event.figure
	 */
	onSelectionChanged : function(emitter, event){
		//this.deleteButton.button( "option", "disabled", event.figure===null );
	},
	
	/**
	 * @method
	 * Sent when an event occurs on the command stack. draw2d.command.CommandStackEvent.getDetail() 
	 * can be used to identify the type of event which has occurred.
	 * 
	 * @template
	 * 
	 * @param {draw2d.command.CommandStackEvent} event
	 **/
	stackChanged:function(event)
	{
		//this.undoButton.button( "option", "disabled", !event.getStack().canUndo() );
		//this.redoButton.button( "option", "disabled", !event.getStack().canRedo() );
	},
	/**画lable**/
	draw_lable:function(){
		var lable=new draw2d.shape.basic.Label({text:"文本测试！文本测试！文本测试！文本测试！文本测试！文本测试！",fontFamily:"SimHei",bgColor:"#ffffff",fontColor:"#123fef",fontSize:20,stroke:0,bgColor:"#CCCCCC", x:150, y:100})
		lable.attr("height",50);
		lable.attr("padding",20);
		lable.installEditor(new draw2d.ui.LabelInplaceEditor());

		this.canvas.add(lable);
	},
	/**画文本**/
	draw_text:function(){
		var shape_text =  new draw2d.shape.basic.Text(
		{
			text:"文本测试！文本测试！文", 
			color:"#FEFFFF",
			bgColor:"#c0c0c0",
			padding:20,
			minHeight:500,
			boundingBox :new draw2d.geo.Rectangle({x:100,y:100,w:100,h:200})
		});
		shape_text.attr("stroke",0);
		shape_text.attr("outlineStroke",0);
		shape_text.attr("resizeable",true);

		// Register a label editor with a dialog
		shape_text.installEditor(new draw2d.ui.LabelInplaceEditor());
		this.canvas.add(shape_text,40,10);
	}
});